package admin

import (
	"html/template"
	"strconv"
	"xianzhi/models"
	"xianzhi/service"
	"xianzhi/utils"
	"xianzhi/utils/wechat"
)

type WechatMenuController struct {
	BaseController
}

func (c *WechatMenuController) Add() {
	p := map[string]interface{}{}
	p["parentId"] = 0
	_, c.Data["topMenuList"] = service.GetWechatMenuList(p)
	c.Data["menuTypeList"] = models.WechatMenuType
	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "admin/wechat_menu/add.html"
}

func (c *WechatMenuController) DoAdd() {
	var res *utils.ResJsonStruct
	input := c.Input()
	if len(input["name"][0]) <= 0 {
		res = utils.ResJson(0, "请输入菜单名", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	mType, _ := strconv.Atoi(input["type"][0])
	if mType == 1 {
		if len(input["key"][0]) <= 0 {
			res = utils.ResJson(0, "请输入菜单关键字", "")
			c.Data["json"] = res
			c.ServeJSON()
			return
		}
	}
	if mType == 2 {
		if len(input["url"][0]) <= 0 {
			res = utils.ResJson(0, "请输入菜单链接", "")
			c.Data["json"] = res
			c.ServeJSON()
			return
		}
	}
	_, err := service.AddWechatMenu(input)
	if err != nil {
		res = utils.ResJson(0, "添加失败", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	res = utils.ResJson(1, "添加成功", "")
	c.Data["json"] = res
	c.ServeJSON()
}

func (c *WechatMenuController) Edit() {
	id := utils.Atoi64(c.Ctx.Input.Param(":id"))
	p := map[string]interface{}{}
	p["id"] = id
	record := service.GetWechatMenuInfo(p)
	c.Data["record"] = record
	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	p = map[string]interface{}{}
	p["parentId"] = 0
	_, c.Data["topMenuList"] = service.GetWechatMenuList(p)
	c.Data["menuTypeList"] = models.WechatMenuType
	c.TplName = "admin/wechat_menu/edit.html"
}

func (c *WechatMenuController) DoEdit() {
	var res *utils.ResJsonStruct
	input := c.Input()
	if len(input["name"][0]) <= 0 {
		res = utils.ResJson(0, "请输入菜单名", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	mType, _ := strconv.Atoi(input["type"][0])
	if mType == 1 {
		if len(input["key"][0]) <= 0 {
			res = utils.ResJson(0, "请输入菜单关键字", "")
			c.Data["json"] = res
			c.ServeJSON()
			return
		}
	}
	if mType == 2 {
		if len(input["url"][0]) <= 0 {
			res = utils.ResJson(0, "请输入菜单链接", "")
			c.Data["json"] = res
			c.ServeJSON()
			return
		}
	}
	_, err := service.EditWechatMenu(input)
	if err != nil {
		res = utils.ResJson(0, "修改失败", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	res = utils.ResJson(1, "修改成功", "")
	c.Data["json"] = res
	c.ServeJSON()
}

func (c *WechatMenuController) ShowAllList() {
	recordList := service.GetAllWechatMenuList()
	c.Data["recordList"] = recordList
	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "admin/wechat_menu/all_list.html"
}

func (c *WechatMenuController) Del() {
	var res *utils.ResJsonStruct
	ids := c.GetString("ids")
	if len(ids) <= 0 {
		res = utils.ResJson(0, "请选择要删除的菜单", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	p := map[string]interface{}{}
	p["ids"] = ids
	_, err := service.DelWechatMenu(p)
	if err != nil {
		res = utils.ResJson(0, "删除失败", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	res = utils.ResJson(1, "删除成功", "")
	c.Data["json"] = res
	c.ServeJSON()
}

func (c *WechatMenuController) EditFiled() {
	input := c.Input()
	service.EditWechatMenu(input)
	res := utils.ResJson(1, "success", "")
	c.Data["json"] = res
	c.ServeJSON()
}

func (c *WechatMenuController) Create() {
	menu := wechat.Menu{}
	oneLevelMenuList := service.GetAllWechatMenuList()
	for _, oneLevelMenu := range oneLevelMenuList {
		one := wechat.OneLevelMenu{}
		one.Name = oneLevelMenu.Name
		if oneLevelMenu.Type == 1 { // 关键字
			one.Type = "click"
			one.Key = oneLevelMenu.Key
		} else if oneLevelMenu.Type == 2 { // 链接
			one.Type = "view"
			one.Url = oneLevelMenu.Url
		}
		for _, twoLevelMenu := range oneLevelMenu.Children {
			two := wechat.TwoLevelMenu{}
			two.Name = twoLevelMenu.Name
			if twoLevelMenu.Type == 1 { // 关键字
				two.Type = "click"
				two.Key = twoLevelMenu.Key
			} else if twoLevelMenu.Type == 2 { // 链接
				two.Type = "view"
				two.Url = twoLevelMenu.Url
			}
			one.SubButton = append(one.SubButton, &two)
		}
		menu.Button = append(menu.Button, &one)
	}
	menu.Create("xianzhi")
	res := utils.ResJson(1, "生成成功", "")
	c.Data["json"] = res
	c.ServeJSON()
}